﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class web_pages_ajax_san_ntd_moi : System.Web.UI.Page
{
    Common comm = new Common();
    public DataTable dtt_sapdienra;
    public DataTable dtt_dangdienra;
    DataTable dtt;
    public string doituong = "";
    public string test = "";
    public string tyle = "";
    public Boolean bGui = true;
    public Boolean guithanhcong = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        doituong = Request.QueryString["id"];
        tyle = Request.QueryString["tyle"];
        if (!IsPostBack)
        {
            
            //Lấy các sàn hiện còn hoạt động, sắp xếp theo thời gian từ đang diễn ra đến chuẩn bị diễn ra:
            string sql = "SELECT * FROM sangiaodich "
                        + " inner join TrungTamViecLam on TrungTamViecLam.ma_trung_tam = sangiaodich.ma_trung_tam "
                        + " WHERE ngay_dien_ra > getdate() and kich_hoat =1 ";
            dtt_sapdienra = comm.getDataTable(sql);

            sql = "SELECT * FROM sangiaodich "
                 + " inner join TrungTamViecLam on TrungTamViecLam.ma_trung_tam = sangiaodich.ma_trung_tam "
                 + " WHERE ngay_dien_ra <= getdate() and kich_hoat =1 ";
            dtt_dangdienra = comm.getDataTable(sql);
            cbb_san.Items.Clear();
            ListItem li;
            if (dtt_dangdienra.Rows.Count > 0)
            {
                for (int i = 0; i < dtt_dangdienra.Rows.Count; i++)
                {
                    li = new ListItem(dtt_dangdienra.Rows[i]["ten_san"].ToString() + " - đang diễn ra", dtt_dangdienra.Rows[i]["ma_san"].ToString());
                    cbb_san.Items.Add(li);
                }
            }
            if (dtt_sapdienra.Rows.Count > 0)
            {
                for (int i = 0; i < dtt_sapdienra.Rows.Count; i++)
                {
                    li = new ListItem(dtt_sapdienra.Rows[i]["ten_san"].ToString() + " - đang diễn ra", dtt_sapdienra.Rows[i]["ma_san"].ToString());
                    cbb_san.Items.Add(li);
                }
            }
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            test = cbb_san.SelectedValue.ToString();
            if (doituong != "")
            {
                string[] arrNhan = doituong.Split(';');
                for (int i = 0; i < arrNhan.Length - 1; i++)
                {
                    string str = "";
                    //Kiểm tra xem là thêm đối tượng nào
                    if (tyle == "vtn")
                    {
                        dtt = comm.getDataTable("select ma_ho_so,ma_doanh_nghiep from ViecLamTrong_ho_so_tuyen_dung " +
                            "where ma_doanh_nghiep='" + arrNhan[i].ToString() + "'");
                        if (dtt.Rows.Count > 0)
                        {
                            for (int j = 0; j < dtt.Rows.Count; j++)
                            {
                                str = "insert into SanGiaoDich_doanh_nghiep(ma_san,ma_doanh_nghiep," +
                                    "ma_ho_so_tuyen_dung)values('" + cbb_san.SelectedItem.Value.ToString() +
                                    "','" + arrNhan[i].ToString() + "','" + dtt.Rows[j][0].ToString() + "')";
                                comm.execNoneQuery(str);
                            }
                        }
                    }
                    else if (tyle == "ntv")
                    {
                        dtt = comm.getDataTable("select ma_ho_so,ma_nguoi from NguoiTimViec_ho_so " +
                            "where ma_nguoi='" + arrNhan[i].ToString() + "' and kich_hoat=1");
                        if (dtt.Rows.Count > 0)
                        {
                            for (int j = 0; j < dtt.Rows.Count; j++)
                            {
                                str = "insert into SanGiaoDich_nguoi_tim_viec(ma_san," +
                                    "ma_ho_so_tim_viec)values('" + cbb_san.SelectedItem.Value.ToString() +
                                    "','" + dtt.Rows[j][0].ToString() + "')";
                                comm.execNoneQuery(str);
                            }
                        }
                    }
                    bGui = false;
                    guithanhcong = true;
                }
            }
        }
        catch
        {
            bGui = false;
            guithanhcong = true;
        }
    }
}
